package com.s4.utils;

import org.apache.ibatis.io.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author XIAOLIANG
 * @ClassName MyBatisUtils.java
 * @Description TODO
 * @createTime 2021年03月05日 20:12:00
 */
public class MyBatisUtils {

	static SqlSessionFactory sqlSessionFactory = null;

	static {   //读取配置文件

		InputStream asStream = null;
		try {
			asStream = Resources.getResourceAsStream("mybatis-config.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(asStream);


}

//创建sqlsession
	public static SqlSession openSession(){
	//创建sqlsession对象
		SqlSession sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
	}

	//关闭sqlSession
	public static void close(SqlSession sqlSession){
		if (sqlSession!=null)
		sqlSession.close();
	}



}
